<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\cms\controller;


use function PHPSTORM_META\type;
use think\admin\Controller;
use think\facade\Config;
use think\admin\service\AdminService;
use think\admin\extend\DataExtend;
use think\admin\service\MenuService;




/**
 * 考核管理
 * Class Eduexam
 * @package app\wechat\controller
 */
class Eduexam extends Controller
{

    /**
     * 当前操作数据库
     * @var string
     */
    protected $table = 'CmsEduExam';
    static $STATE_OPTION = array(1=>"报名中",2=>"已报名",3=>"已取消");
    public $type = array(1=>"岗位考核",2=>"技能考核",3=>"其它考核");
    public $scale = array(1=>"1-20人小班",2=>"20-50人中班",3=>"100人以上大班");
    public $superviser = array(1=>"湖南省厅",2=>"长沙市局");
    public $city = array(1=>"湖南省",2=>"长沙市",3=>"娄底市",4=>"株洲市",5=>"湘潭市",6=>"衡阳市",7=>"邵阳市",
        8=>"岳阳市",9=>"常德市",10=>"张家界市",11=>"益阳市",12=>"郴州市",13=>"永州市",14=>"怀化市",15=>"湘西土家族苗族自治州");


    /**
     * 考核管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = "考核管理";
        $map = [];
        //根据like后的字段来进行搜索
        $this->state = self::$STATE_OPTION;
        $this->status = intval($this->request->get("status",-1));
        if($this->status>=0) $map['status'] = $this->status;
        $query=$this->app->db->name($this->table)->where($map)
            ->order('id desc')->select()->toArray();
        $rowAt=time();
        $data['status'] = 2;
        for($i=0;$i<count($query);$i++){
           // var_dump($query[$i]['enroll_end_at']);

            if(strtotime($query[$i]['enroll_end_at'])<=$rowAt){
                $maps=['id'=>$query[$i]['id']];
                $this->app->db->name($this->table)->where($maps)->update($data);
            }
        }
        $this->_query($this->table)->like('name')->where($map)->order('id desc')->page();
    }
//    public function updateAt($id){//更新时间
//
//        $data['status'] = 2;
//        $map=['id'=>$id];
//        $this->app->db->name($this->table)->where($map)->update($data);
//
//    }

    public function _getEduPosition(){
        $position = $this->app->db->name('CmsEduWork')->where(["parent_id"=>0])
            ->order('id asc')->select()->toArray();
        for($i=0; $i<count($position); $i++){
            $map = ["parent_id"=>$position[$i]['id']];
            $position[$i]['sub'] = $this->app->db->name('CmsEduWork')->where($map)
                                ->order('id desc')->select()->toArray();
        }
        return $position;
    }

    public function widget()
    {
        $map = [];
        $query = $this->_query($this->table)->like('name')->where($map)->order('id desc');
        $query->whereNotIn('id', ['subscribe', 'default'])->order('id desc')->page();
    }

    public function ajaxSearch()
    {
        $limit = 5;
        $page = intval($this->request->request('page',1));
        $offset = ($page-1)*$limit;
        $map = ["status"=>1];
        $name = $this->request->request('name');
        if($name!=""){
            $category = $this->app->db->name()
                ->where($map)
                ->whereLike("name","%$name%")
                ->order("id desc")
                ->limit($offset,$limit)->select()->toArray();
            $total = $this->app->db->name($this->table)
                ->where($map)
                ->whereLike("name","%$name%")->count();
        }else{
            $category = $this->app->db->name($this->table)
                ->where($map)
                ->order("id desc")
                ->limit($offset,$limit)->select()->toArray();
            $total = $this->app->db->name($this->table)->where($map)->count();
        }
        $this->success("成功",array("list"=>$category,"total"=>$total,"limit"=>$limit));
    }

    /**
     * 表单数据处理
     * @param array $vo
     * @throws \ReflectionException
     */
    protected function _form_filter(&$vo)
    {
        if ($this->request->isGet()) {
            // 清理权限节点
            AdminService::instance()->clearCache();
            $this->position = $this->_getEduPosition();
            if($vo)
                $this->position_ids = $vo['position_ids'];
            else
                $this->position_ids = "";
        }
    }

    /**
     * 添加考核
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function add()
    {

        if ($this->request->isGet()) {
            $this->_applyFormToken();
            $this->_form($this->table, 'form');
        } else {
            $update = $this->request->post('data', []);
            $update[ 'create_by']   = AdminService::instance()->getUserId();
            $update[ 'update_at'] = $update[ 'create_at'] = time();
            $id = $this->app->db->name($this->table)->insertGetId($update);

            if ($id !== false) {
                $this->success('添加成功！','javascript:history.back()');
            } else {
                $this->error('添加失败，请稍候再试！');
            }
        }
    }

    /**
     * 编辑考核
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->id = $this->request->get('id');
        $map = array("id" => $this->id);
        if (empty($this->id)) $this->error('参数错误，请稍候再试！');
        if ($this->request->isGet()) {
            if ($this->request->get('output') === 'json') {
                $data = $this->app->db->name($this->table)->where($map)->find();
                $this->success('获取数据成功！', $data);
            } else {
                $this->title = '编辑信息';
                $this->_applyFormToken();
                $this->_form($this->table, 'form');
            }
        }else{

            $data = $this->request->post('data', []);
            $data['update_at'] = time();
            if ($this->app->db->name($this->table)->where($map)->update($data) !== false) {
                $this->success('信息更新成功！', 'javascript:history.back()');
            } else {
                $this->error('信息更新失败，请稍候再试！');
            }
        }
    }

    /**
     * 删除考核
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $id = $this->request->request('id');
        $map = array("class_id"=>$id);
        if($this->app->db->name("CmsEduExamEnroll")->where($map)->find()){
            $this->error("已存在报名信息，无法删除");
        }
        $this->_delete($this->table);
    }

    /**
     * 取消考核
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function cancelEdu()
    {
        $this->id = $this->request->get('id');
        $map = array("id" => $this->id);
        $data = array("status" => 3);
        if ($this->app->db->name($this->table)->where($map)->update($data) !== false) {
            $this->success('操作成功！');
        }
        $this->error('操作失败，请稍候再试！');
    }

    /**
     * 结束报名
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function endEnroll()
    {
        $this->id = $this->request->get('id');
        $map = array("id" => $this->id);
        $data = array("status" => 2);
        if ($this->app->db->name($this->table)->where($map)->update($data) !== false) {
            $this->success('操作成功！');
        }
        $this->error('操作失败，请稍候再试！');
    }
}
